package com.sxfq.loansupermarket.backsystem.modular.refunds.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.refunds.dao.RefundDao;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.RechargeInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.RefundInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.SearchRefundInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.service.IRefundService;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RefundServiceImpl implements IRefundService {

    @Autowired
    private RefundDao dao;

    @Autowired
    ICustomerService customerService;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<RefundInfo> selectPageListRefundInfo(Page<RefundInfo> page, SearchRefundInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRecordTime())) {
            params.put("startRecordTime", searchInfo.getStartRecordTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRecordTime())) {
            params.put("endRecordTime", searchInfo.getEndRecordTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsRefund())) {
            params.put("isRefund", searchInfo.getIsRefund());
        }
        if(!"请选择".equals(searchInfo.getOneChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if(!"请选择".equals(searchInfo.getTwoChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if(!"请选择".equals(searchInfo.getThrChannel())){
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if ("1".equals(userDto.getRoleid())) {
            params.put("entryClerk", "");
        } else {
            params.put("entryClerk", ShiroKit.getUser().getAccount());
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "refund_info";
        params.put("tableName", this.getTableName());
        params.put("tableName1", tableName1);
        return dao.getQueryPageRefundInfo(page, params);
    }

    @Transactional
    public void delete(String id) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String tableName1 = tableHead + "refund_info";
        String selectSql="select * from "+tableName1+" where id = '" + id + "'";
        RefundInfo refundInfo=sqlMapper.selectOne(selectSql,RefundInfo.class);
        String updateSql="update "+tableName+" a set a.actual_account_balance=actual_account_balance+'"+refundInfo.getRefundMoney()+"'" +
                "  where reservations_id='"+refundInfo.getReservationsId()+"'";
        String sql = "delete from "+tableName1+" where id = '" + id + "'";
        if("是".equals(refundInfo.getIsRefund())){
            sqlMapper.update(updateSql);
        }
        sqlMapper.delete(sql);
    }

    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }
}
